package be.gnx.fogo.application.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Person {

  private Long id;

  private Firstname alternativeName;

  private Surname surname;

  private Sexe sexe;

  private Person spouse;

  private Person mother;

  private Person father;

  private Integer dateOfBirthYear;

  private Integer dateOfBirthMonth;

  private Integer dateOfBirthDay;

  private Integer dateOfDeathYear;

  private Integer dateOfDeathMonth;

  private Integer dateOfDeathDay;

  private Set<Category> categorySet = new HashSet<Category>();

  private Set<Communication> communicationSet = new HashSet<Communication>();

  private List<Givenname> givennameList = new ArrayList<Givenname>();

  private Set<ExtraInfo> extraInfoSet = new HashSet<ExtraInfo>();

  private Set<Person> fatherChildren = new HashSet<Person>();

  private Set<Person> motherChildren = new HashSet<Person>();

  public Person getInhabitantFather() {
    Address address = null;
    if (getCommunicationSet() != null) {
      for (Communication communication : getCommunicationSet()) {
        if (communication.getCategory().getName().equals("Primary Personal Address")) {
          if (communication.getAddress() != null) {
            address = communication.getAddress();
          }
        }
      }
    }

    if (address != null) {
      if (getFather() != null) {
        if (getFather().getCommunicationSet() != null) {
          for (Communication communication : getFather().getCommunicationSet()) {
            if (communication.getCategory().getName().equals("Primary Personal Address")) {
              if (communication.getAddress() != null) {
                if (communication.getAddress().equals(address)) {
                  return getFather();
                }
              }
            }
          }
        }
      }
    }
    if (getSexe().getCode() == 'F') {
      if (address != null) {
        if (getSpouse() != null) {
          if (getSpouse().getCommunicationSet() != null) {
            for (Communication communication : getSpouse().getCommunicationSet()) {
              if (communication.getCategory().getName().equals("Primary Personal Address")) {
                if (communication.getAddress() != null) {
                  if (communication.getAddress().equals(address)) {
                    return getSpouse();
                  }
                }
              }
            }
          }
        }
      }
    } else {
      return this;
    }
    return null;
  }

  public Person getInhabitantMother() {
    Address address = null;
    if (getCommunicationSet() != null) {
      for (Communication communication : getCommunicationSet()) {
        if (communication.getCategory().getName().equals("Primary Personal Address")) {
          if (communication.getAddress() != null) {
            address = communication.getAddress();
          }
        }
      }
    }

    if (address != null) {
      if (getMother() != null) {
        if (getMother().getCommunicationSet() != null) {
          for (Communication communication : getMother().getCommunicationSet()) {
            if (communication.getCategory().getName().equals("Primary Personal Address")) {
              if (communication.getAddress() != null) {
                if (communication.getAddress().equals(address)) {
                  return getMother();
                }
              }
            }
          }
        }
      }
    }
    if (getSexe().getCode() == 'M') {
      if (address != null) {
        if (getSpouse() != null) {
          if (getSpouse().getCommunicationSet() != null) {
            for (Communication communication : getSpouse().getCommunicationSet()) {
              if (communication.getCategory().getName().equals("Primary Personal Address")) {
                if (communication.getAddress() != null) {
                  if (communication.getAddress().equals(address)) {
                    return getSpouse();
                  }
                }
              }
            }
          }
        }
      }
    } else {
      return this;
    }
    return null;
  }

  public Set<Person> getInhabitantChildren() {
    Set<Person> inhabitantChildren = new HashSet<Person>();
    Person inhabitantFather = getInhabitantFather();
    if (inhabitantFather != null) {
      Set<Person> fatherChildren = inhabitantFather.getChildren();
      for (Person child : fatherChildren) {
        if (!inhabitantChildren.contains(child)) {
          Address address = null;
          if (inhabitantFather.getCommunicationSet() != null) {
            for (Communication communication : inhabitantFather.getCommunicationSet()) {
              if (communication.getCategory().getName().equals("Primary Personal Address")) {
                if (communication.getAddress() != null) {
                  address = communication.getAddress();
                }
              }
            }
          }
          if (address != null) {
            if (child.getCommunicationSet() != null) {
              for (Communication communication : child.getCommunicationSet()) {
                if (communication.getCategory().getName().equals("Primary Personal Address")) {
                  if (communication.getAddress() != null) {
                    if (communication.getAddress().equals(address)) {
                      inhabitantChildren.add(child);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

    Person inhabitantMother = getInhabitantMother();
    if (inhabitantMother != null) {
      Set<Person> motherChildren = inhabitantMother.getChildren();
      for (Person child : motherChildren) {
        if (!inhabitantChildren.contains(child)) {
          Address address = null;
          if (inhabitantMother.getCommunicationSet() != null) {
            for (Communication communication : inhabitantMother.getCommunicationSet()) {
              if (communication.getCategory().getName().equals("Primary Personal Address")) {
                if (communication.getAddress() != null) {
                  address = communication.getAddress();
                }
              }
            }
          }
          if (address != null) {
            if (child.getCommunicationSet() != null) {
              for (Communication communication : child.getCommunicationSet()) {
                if (communication.getCategory().getName().equals("Primary Personal Address")) {
                  if (communication.getAddress() != null) {
                    if (communication.getAddress().equals(address)) {
                      inhabitantChildren.add(child);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    return inhabitantChildren;
  }

  public Set<Person> getChildren() {
    if (getSexe().getCode() == 'F') {
      return getMotherChildren();
    } else {
      return getFatherChildren();
    }
  }

  public void setChildren(Set<Person> children) {
    if (getSexe().getCode() == 'F') {
      this.motherChildren = children;
    } else {
      this.fatherChildren = children;
    }
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public Firstname getAlternativeName() {
    return alternativeName;
  }

  public void setAlternativeName(Firstname alternativeName) {
    this.alternativeName = alternativeName;
  }

  public Surname getSurname() {
    return surname;
  }

  public void setSurname(Surname surname) {
    this.surname = surname;
  }

  public Sexe getSexe() {
    return sexe;
  }

  public void setSexe(Sexe sexe) {
    this.sexe = sexe;
  }

  public Person getSpouse() {
    return spouse;
  }

  public void setSpouse(Person spouse) {
    this.spouse = spouse;
  }

  public Person getMother() {
    return mother;
  }

  public void setMother(Person mother) {
    this.mother = mother;
  }

  public Person getFather() {
    return father;
  }

  public void setFather(Person father) {
    this.father = father;
  }

  public Integer getDateOfBirthYear() {
    return dateOfBirthYear;
  }

  public void setDateOfBirthYear(Integer dateOfBirthYear) {
    this.dateOfBirthYear = dateOfBirthYear;
  }

  public Integer getDateOfBirthMonth() {
    return dateOfBirthMonth;
  }

  public void setDateOfBirthMonth(Integer dateOfBirthMonth) {
    this.dateOfBirthMonth = dateOfBirthMonth;
  }

  public Integer getDateOfBirthDay() {
    return dateOfBirthDay;
  }

  public void setDateOfBirthDay(Integer dateOfBirthDay) {
    this.dateOfBirthDay = dateOfBirthDay;
  }

  public Integer getDateOfDeathYear() {
    return dateOfDeathYear;
  }

  public void setDateOfDeathYear(Integer dateOfDeathYear) {
    this.dateOfDeathYear = dateOfDeathYear;
  }

  public Integer getDateOfDeathMonth() {
    return dateOfDeathMonth;
  }

  public void setDateOfDeathMonth(Integer dateOfDeathMonth) {
    this.dateOfDeathMonth = dateOfDeathMonth;
  }

  public Integer getDateOfDeathDay() {
    return dateOfDeathDay;
  }

  public void setDateOfDeathDay(Integer dateOfDeathDay) {
    this.dateOfDeathDay = dateOfDeathDay;
  }

  public Set<Category> getCategorySet() {
    return categorySet;
  }

  public void setCategorySet(Set<Category> categorySet) {
    this.categorySet = categorySet;
  }

  public List<Givenname> getGivennameList() {
    return givennameList;
  }

  public void setGivennameList(List<Givenname> givennameList) {
    this.givennameList = givennameList;
  }

  public Set<Communication> getCommunicationSet() {
    return communicationSet;
  }

  public void setCommunicationSet(Set<Communication> communicationSet) {
    this.communicationSet = communicationSet;
  }

  public Set<ExtraInfo> getExtraInfoSet() {
    return extraInfoSet;
  }

  public void setExtraInfoSet(Set<ExtraInfo> extraInfoSet) {
    this.extraInfoSet = extraInfoSet;
  }

  public Set<Person> getFatherChildren() {
    return fatherChildren;
  }

  public void setFatherChildren(Set<Person> fatherChildren) {
    this.fatherChildren = fatherChildren;
  }

  public Set<Person> getMotherChildren() {
    return motherChildren;
  }

  public void setMotherChildren(Set<Person> motherChildren) {
    this.motherChildren = motherChildren;
  }

}
